package com.yxhl.zoume.core.func.appraisal.ui.fragment;

import android.databinding.DataBindingUtil;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yxhl.zoume.AppraisalConfirmDataBinding;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.func.appraisal.model.AppraisalDriverInfo;
import com.yxhl.zoume.core.func.appraisal.presenter.AppraisalResultPresenter;
import com.yxhl.zoume.core.func.appraisal.ui.activity.AppraisalConfirmActivity;
import com.yxhl.zoume.core.func.appraisal.ui.adapter.AppraisalLabelConformAdapter;
import com.yxhl.zoume.core.func.appraisal.view.AppraisalResultView;
import com.yxhl.zoume.data.http.rest.response.appraisal.AppraisalResultResponse;
import com.yxhl.zoume.di.component.appraisal.AppraisalConfirmComponent;
import com.yxhl.zoume.utils.DensityUtils;
import com.yxhl.zoume.utils.PromptUtils;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by vhawk on 16/9/18.
 */
public class AppraisalConfirmFragment extends BaseDataBindingFragment implements AppraisalResultView {

    private static final String KEY_DRIVER_INFO = "KEY_DRIVER_INFO";
    @Inject
    public AppraisalResultPresenter presenter;
    @BindView(R.id.recycle_appraisal)
    RecyclerView appraisalLabelRecycleView;
    private AppraisalConfirmDataBinding appraisalConfirmDataBinding;
    private String orderId;
    private AppraisalConfirmComponent component;

    public static Fragment newInstance(AppraisalDriverInfo driverInfo) {
        Fragment fragment = new AppraisalConfirmFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_DRIVER_INFO, driverInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        handleExtraParam();
        presenter.attachView(this);
        presenter.fetchConfirmResult(orderId);
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        presenter.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    public void handleAppraisalResultSucceed(AppraisalResultResponse response) {
        appraisalConfirmDataBinding.setAppraisalResult(response);
        GridLayoutManager girdLayoutManager = new GridLayoutManager(component.context(), 3);
        AppraisalLabelConformAdapter adapter = new AppraisalLabelConformAdapter(response.getLabels());
        appraisalLabelRecycleView.setAdapter(adapter);
        appraisalLabelRecycleView.setLayoutManager(girdLayoutManager);
        appraisalLabelRecycleView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(DensityUtils.dip2px(component.context(), 2),
                        DensityUtils.dip2px(component.context(), 2),
                        DensityUtils.dip2px(component.context(), 2),
                        DensityUtils.dip2px(component.context(), 2));
            }
        });
    }

    @Override
    public void handleAppraisalFailure(String msg) {
        PromptUtils.showShort(component.context(), msg);
    }

    @Override
    public void handleAppraisalError() {
        PromptUtils.showShort(component.context(), R.string.net_error);
    }

    private void handleExtraParam() {
        Bundle arguments = getArguments();
        AppraisalDriverInfo driverInfo = (AppraisalDriverInfo) arguments.getSerializable(KEY_DRIVER_INFO);
        orderId = driverInfo.getOrderId();
        appraisalConfirmDataBinding.setDriverInfo(driverInfo);
    }

    @Override
    protected void initializeInjector() {
        component = getComponent(AppraisalConfirmComponent.class);
        component.inject(this);
    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        appraisalConfirmDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_appraisal_confirm, container, false);
        return appraisalConfirmDataBinding.getRoot();
    }

    @Override
    protected void setupToolBar() {
        AppraisalConfirmActivity activity = (AppraisalConfirmActivity) mActivity;
        activity.setToolbarTitle("评价");
    }
}
